<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户uid">
          <el-input placeholder="用户uid" v-model="form.uid"></el-input>
        </el-form-item>
        <el-form-item label="IP地址">
          <el-input placeholder="用户IP" v-model="form.ip"></el-input>
        </el-form-item>
        <el-form-item label="手机系统">
          <el-input placeholder="手机系统" v-model="form.phone_os"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input placeholder="设备ID">
            <el-input placeholder="设备id" v-model="form.device_id"></el-input>
          </el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="form.timeData" type="datetimerange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="pickerDefaultTime" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="uid" label="用户uid" align="center" />
          <el-table-column prop="phone_type" label="设备类型" align="center" width="150" />
          <el-table-column prop="phone_os" label="手机系统" align="center" width="150" />
          <el-table-column prop="ip" label="ip地址" align="center" width="130"/>
          <el-table-column prop="createtime" label="登录时间" align="center" width="180" :formatter="formatterTime" />
          <el-table-column prop="source_from" label="用户来源" align="center" />
          <el-table-column prop="app_version" label="APP版本" align="center" />
          <el-table-column prop="device_id" label="设备ID" align="center" width="200" />
          <el-table-column prop="device_ua" label="UA" align="center" />
          <el-table-column prop="device_mac" label="MAC地址" align="center" />
          <el-table-column prop="device_adid" label="ADID" align="center" />
          <el-table-column prop="device_oaid" label="OAID" align="center" />
          <el-table-column prop="device_imei" label="IMEI" align="center" />
        </el-table>
        <div class="table-pagination">
          <el-pagination :current-page="pageable.pageNum" :page-size="pageable.pageSize" :page-sizes="[10, 25, 50, 100]"
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="pageable.total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, exportToExcel, timestamp, pickerDefaultTime } from "@/utils/utils";
import { userLoginListApi } from "@/api/moby/user/user";

const loading = ref(false);

const tableData = ref([]);

// const loading = ref(false);

// const tableData = ref([]);
const formData = {
  ip: "",
  phone_os: "",
  device_id: "",
  timeData: [],
  uid: "",
};
const form = reactive({ ...formData });

const pageable = reactive({
  pageNum: 1,
  pageSize: 20,
  total: 0
});

// 参数
const param = () => {
  return {
    uid: form.uid,
    ip: form.ip,
    phone_os: form.phone_os,
    device_id: form.device_id,
    start_time: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end_time: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
    page: pageable.pageNum,
    pagesize: pageable.pageSize
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await userLoginListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
//查询
const onQuery = async () => {
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();

};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await userLoginListApi(data);

  let head = {
    id: "用户id",
    uid: "用户uid",
    // ip: "ip",
    // account_name: "账号",
    phone_type: "设备类型",
    phone_os: "设备系统版本号",
    ip: "ip地址",
    app_version: "APP版本",
    device_id: "设备ID",
    device_ua: "UA",
    device_mac: "MAC地址",
    device_adid: "ADID",
    device_oaid: "OAID",
    device_imei: "IMEI",
    source_from: "用户来源",
    createtime: "登录时间",
    updatetime: "最后一次登录时间"
  };
  exportToExcel(res.data.results, head, "Mobybox-登录信息");
};



// 条数
const handleSizeChange = number => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = number => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
